package cn.tedu.luban.order.main.converter;

import cn.tedu.luban.order.common.enums.OrderStatusEnum;
import cn.tedu.luban.order.protocol.VO.AdminOrderLogVO;
import cn.tedu.luban.order.protocol.bo.OrderLogBO;
import cn.tedu.luban.order.protocol.dos.OrderLogDO;
import cn.tedu.luban.order.protocol.param.OrderCreateParam;
import cn.tedu.luban.order.protocol.param.OrderLogParam;
import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import springfox.documentation.spi.service.contexts.SecurityContext;

@Component
public class OrderLogConverter {
    public List<AdminOrderLogVO> bos2vos(List<OrderLogBO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<AdminOrderLogVO> adminOrderLogVOList = new ArrayList<>(list.size());
        for (OrderLogBO orderLogBo : list) {
            AdminOrderLogVO adminOrderLogVo = this.bo2vo(orderLogBo);
            adminOrderLogVOList.add(adminOrderLogVo);
        }
        return adminOrderLogVOList;
    }
    public AdminOrderLogVO bo2vo(OrderLogBO bo) {
        AdminOrderLogVO orderLog = new AdminOrderLogVO();
        if (bo == null) {
            return orderLog;
        }
        BeanUtils.copyProperties(bo, orderLog);
        return orderLog;
    }
    public List<OrderLogBO> dos2bos(List<OrderLogDO> list) {
        List<OrderLogBO> orderLogBos = new ArrayList<>(list.size());
        for (OrderLogDO orderLog : list) {
            orderLogBos.add(this.do2bo(orderLog));
        }
        return orderLogBos;
    }
    public OrderLogBO do2bo(OrderLogDO orderLog) {
        OrderLogBO orderLogBO = new OrderLogBO();
        if (orderLog == null) {
            return orderLogBO;
        }
        BeanUtils.copyProperties(orderLog, orderLogBO);
        return orderLogBO;
    }

    public OrderLogParam assembleOrderLogParam(Long userId,String orderNo, OrderStatusEnum orderStatusEnum) {
        OrderLogParam logParam = new OrderLogParam();
        logParam.setOrderNo(orderNo);
        logParam.setUserId(userId);
        logParam.setOperateName(orderStatusEnum.getMessage());
        logParam.setRemark(orderStatusEnum.getMessage());
        long now = System.currentTimeMillis();
        logParam.setOperateTime(now);
        logParam.setGmtModified(now);
        logParam.setGmtCreate(now);
        return logParam;
    }

    public OrderLogDO param2do(OrderLogParam param) {
        OrderLogDO orderLog = new OrderLogDO();
        if (param==null){
            return new OrderLogDO();
        }
        BeanUtils.copyProperties(param, orderLog);
        UserPrinciple loginUser = LubanSecurityContext.getLoginToken();
        orderLog.setGmtCreate(System.currentTimeMillis());
        orderLog.setGmtModified(orderLog.getGmtCreate());
        orderLog.setCreateUserId(loginUser != null ? loginUser.getId() : 0);
        orderLog.setModifiedUserId(loginUser != null ?  loginUser.getId() : 0);
        orderLog.setStatus(1);
        orderLog.setCreateUserName(loginUser != null ? loginUser.getUsername() : "mock");
        orderLog.setModifiedUserName(loginUser != null ? loginUser.getUsername() : "mock");
        return orderLog;
    }
}
